package com.ctms.system.controller;

import com.ctms.common.utils.*;
import com.ctms.system.domain.*;
import com.ctms.system.service.CourseService;
import com.ctms.system.service.CourseStudentService;
import com.ctms.system.service.EvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("/evaluation")
@Controller
//评价
public class EvaluationController {

    @Autowired
    EvaluationService evaluationService;
    @Autowired
    CourseService courseService;
    @Autowired
    CourseStudentService courseStudentService;

    //学生对教师及课程进行评价
    @GetMapping("/listStudent")
    public String listStudent(){
        return "sys/evaluation/listStudent";
    }

    @GetMapping("/student")
    @ResponseBody
    PageUtils student(@RequestParam Map<String,Object> params) throws Exception{
        UserDO userDO = ShiroUtils.getUser();
        Integer loginId = Integer.parseInt(Long.toString(userDO.getUserId()));
        params.put("loginId",loginId);
        Query query = new Query(params);
        List<UserCourseDo> userCourseDos = evaluationService.selectAllTeaEvaluation(params);
        for (int i=0;i<userCourseDos.size();i++){
            CourseDO courseDO = courseService.selectCourseByNo(userCourseDos.get(i).getCourseNo());
            String time1 = courseDO.getStartDate();
            String time2 = courseDO.getEndDate();
            //课程名称
            userCourseDos.get(i).setCourseName(courseDO.getCourseName());
            userCourseDos.get(i).setStartDate(time1);
            userCourseDos.get(i).setEndDate(time2);
            //如果想比较日期则写成"yyyy-MM-dd"就可以了
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            //将字符串形式的时间转化为Date类型的时间
            //开始时间
            Date a=sdf.parse(time1);
            //结束时间
            Date b=sdf.parse(time2);
            //当前时间
            Date date = new Date();
            String d = sdf.format(date);
            Date c = sdf.parse(d);
            Integer value = DateCompareUtils.isEffectiveDate(c,a,b);
            userCourseDos.get(i).setStatus(value);
            //教师名称
            Integer teacherId=userCourseDos.get(i).getUserId();
            String teacherName = courseService.getNameById(teacherId);
            userCourseDos.get(i).setName(teacherName);
        }
        int total = evaluationService.countTea(query);
        PageUtils pageUtils = new PageUtils(userCourseDos,total);
        return pageUtils;
    }

    @GetMapping("/evaluationStudent/{value}")
    public String evaluationStudent(Model model, @PathVariable("value")Integer value){
        System.out.println(value);
        Integer id = value;
        UserCourseDo userCourseDo = evaluationService.selectById(id);
       //课程名
        String courseNo = userCourseDo.getCourseNo();
        CourseDO courseDO = courseService.selectCourseByNo(courseNo);
        String courseName = courseDO.getCourseName();
        int cid = courseDO.getCid();
       //教师名
        int teacher_id = userCourseDo.getUserId();
        String teacherName = courseService.getNameById(teacher_id);
        model.addAttribute("userCourseId",id);
        model.addAttribute("cid",cid);
        model.addAttribute("teacherId",teacher_id);
        model.addAttribute("courseName",courseName);
        model.addAttribute("teacherName",teacherName);
        return "sys/evaluation/evaluationStudent";
    }

    @PostMapping("/saveStudent")
    @ResponseBody
    R saveStudent(EvaluationDO evaluationDO){
        //获取教师课程信息id
        int id = evaluationDO.getUserCourseId();
        //通过id获取教师课程的所有信息
        UserCourseDo userCourseDo = evaluationService.selectById(id);
        System.out.println(id);
        //获取登录对象
        UserDO userDO = ShiroUtils.getUser();
        //获取评价人id
        int evaluationPeople = Integer.parseInt(Long.toString(userDO.getUserId()));
        //评价人id
        evaluationDO.setEvaluationPeople(evaluationPeople);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String time = sdf.format(date);
        //评价时间
        evaluationDO.setEvaluationTime(time);
        if (evaluationService.insertStuToTea(evaluationDO)>0){
            //评价成功之后将教师课程信息表中对应的flag属性置为1
            userCourseDo.setFlag(1);
            evaluationService.updateFlagById(userCourseDo);
            return R.ok();
        }else {
            //评价失败
            return R.error();
        }
    }

    //教师对学生进行评价
    @GetMapping("/listTeacher")
    public String listTeacher(){
        return "sys/evaluation/listTeacherCourse";
    }

    @GetMapping("/teacher")
    @ResponseBody
    PageUtils teacher(@RequestParam Map<String,Object> params) throws Exception{
        UserDO userDO = ShiroUtils.getUser();
        int loginId = Integer.parseInt(Long.toString(userDO.getUserId()));
        params.put("loginId",loginId);
        //传过来的查询内容
        String course_name = String.valueOf(params.get("condition"));
        CourseDO courseDO1 = courseService.selectCourseByCid(course_name);
        if (courseDO1 == null ){
            Query query = new Query(params);
            List<CourseStudentDO> courseStudentDOS = evaluationService.selectAllStuEvaluation(params);
            for (int i=0;i<courseStudentDOS.size();i++){
                int cid = courseStudentDOS.get(i).getCid();
                CourseDO courseDO = courseService.selectById(cid);
                String time1 = courseDO.getStartDate();
                String time2 = courseDO.getEndDate();
                //课程名称
                courseStudentDOS.get(i).setCourseName(courseDO.getCourseName());
                courseStudentDOS.get(i).setStartDate(time1);
                courseStudentDOS.get(i).setEndDate(time2);
                //如果想比较日期则写成"yyyy-MM-dd"就可以了
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                //将字符串形式的时间转化为Date类型的时间
                //开始时间
                Date a=sdf.parse(time1);
                //结束时间
                Date b=sdf.parse(time2);
                //当前时间
                Date date = new Date();
                String d = sdf.format(date);
                Date c = sdf.parse(d);
                Integer value = DateCompareUtils.isEffectiveDate(c,a,b);
                courseStudentDOS.get(i).setStatus(value);
                //学生id
                int stu_id = courseStudentDOS.get(i).getUserId();
                String name = courseService.getNameById(stu_id);
                courseStudentDOS.get(i).setName(name);
            }
            int total = evaluationService.countStu(query);
            PageUtils pageUtils = new PageUtils(courseStudentDOS,total);
            return pageUtils;
        }else {
            int courseId = courseDO1.getCid();
            params.put("courseId",courseId);
            Query query = new Query(params);
            List<CourseStudentDO> courseStudentDOS = evaluationService.selectAllStuEvaluation(params);
            for (int i=0;i<courseStudentDOS.size();i++){
                int cid = courseStudentDOS.get(i).getCid();
                CourseDO courseDO = courseService.selectById(cid);
                String time1 = courseDO.getStartDate();
                String time2 = courseDO.getEndDate();
                //课程名称
                courseStudentDOS.get(i).setCourseName(courseDO.getCourseName());
                courseStudentDOS.get(i).setStartDate(time1);
                courseStudentDOS.get(i).setEndDate(time2);
                //如果想比较日期则写成"yyyy-MM-dd"就可以了
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                //将字符串形式的时间转化为Date类型的时间
                //开始时间
                Date a=sdf.parse(time1);
                //结束时间
                Date b=sdf.parse(time2);
                //当前时间
                Date date = new Date();
                String d = sdf.format(date);
                Date c = sdf.parse(d);
                Integer value = DateCompareUtils.isEffectiveDate(c,a,b);
                courseStudentDOS.get(i).setStatus(value);
                //学生id
                int stu_id = courseStudentDOS.get(i).getUserId();
                String name = courseService.getNameById(stu_id);
                courseStudentDOS.get(i).setName(name);
            }
            int total = evaluationService.countStu(query);
            PageUtils pageUtils = new PageUtils(courseStudentDOS,total);
            return pageUtils;
        }
    }

    @GetMapping("/evaluationTeacher/{value}")
    public String evaluationTeacher(Model model, @PathVariable("value")Integer value){
        System.out.println(value);
        Integer id = value;
        CourseStudentDO courseStudentDO = courseStudentService.selectById(id);
        //课程名
        CourseDO courseDO = courseService.selectById(courseStudentDO.getCid());
        String courseName = courseDO.getCourseName();
        int cid = courseDO.getCid();
        //学生名字
        int stu_id = courseStudentDO.getUserId();
        String name = courseService.getNameById(stu_id);

        model.addAttribute("courseStuId",id);
        model.addAttribute("cid",cid);
        model.addAttribute("studentId",stu_id);
        model.addAttribute("courseName",courseName);
        model.addAttribute("studentName",name);
        return "sys/evaluation/evaluationTeacher";
    }

    @PostMapping("/saveTeacher")
    @ResponseBody
    R saveTeacher(EvaluationDO evaluationDO){
        int id = evaluationDO.getCourseStuId();
        CourseStudentDO courseStudentDO = courseStudentService.selectById(id);
        System.out.println(id);
        UserDO userDO = ShiroUtils.getUser();
        int evaluationPeople = Integer.parseInt(Long.toString(userDO.getUserId()));
        //评价人id
        evaluationDO.setEvaluationPeople(evaluationPeople);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String time = sdf.format(date);
        //评价时间
        evaluationDO.setEvaluationTime(time);
        if (evaluationService.insertStuToTea(evaluationDO)>0){
            courseStudentDO.setFlag(1);
            evaluationService.updateFlagBySid(courseStudentDO);
            return R.ok();
        }else {
            return R.error();
        }
    }

    //学生意见反馈列表
    @GetMapping("/stuFeedback")
    public String stuFeedback(){
        return "sys/feedback/stuFeedback";
    }

    @GetMapping("/listStuFeedback")
    @ResponseBody
    PageUtils listStuFeedback(@RequestParam Map<String,Object> params) throws Exception{
        UserDO userDO = ShiroUtils.getUser();
        int loginId = Integer.parseInt(Long.toString(userDO.getUserId()));
        params.put("loginId",loginId);
        //传过来的查询内容
        String course_name = String.valueOf(params.get("condition"));
        CourseDO courseDO1 = courseService.selectCourseByCid(course_name);
        //没有查询条件
        if (courseDO1 == null ){
            Query query = new Query(params);
            List<CourseStudentDO> courseStudentDOS = evaluationService.selectStuFeedback(params);
            for (int i=0;i<courseStudentDOS.size();i++){
                int cid = courseStudentDOS.get(i).getCid();
                CourseDO courseDO = courseService.selectById(cid);
                String time1 = courseDO.getStartDate();
                String time2 = courseDO.getEndDate();
                //课程名称
                courseStudentDOS.get(i).setCourseName(courseDO.getCourseName());
                courseStudentDOS.get(i).setStartDate(time1);
                courseStudentDOS.get(i).setEndDate(time2);
                //如果想比较日期则写成"yyyy-MM-dd"就可以了
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                //将字符串形式的时间转化为Date类型的时间
                //开始时间
                Date a=sdf.parse(time1);
                //结束时间
                Date b=sdf.parse(time2);
                //当前时间
                Date date = new Date();
                String d = sdf.format(date);
                Date c = sdf.parse(d);
                Integer value = DateCompareUtils.isEffectiveDate(c,a,b);
                courseStudentDOS.get(i).setStatus(value);
                //学生id
                int stu_id = courseStudentDOS.get(i).getUserId();
                String name = courseService.getNameById(stu_id);
                courseStudentDOS.get(i).setName(name);
                EvaluationDO evaluationDO = evaluationService.selectByUserId(stu_id,loginId);
                if (evaluationDO == null){
                    //未评价
                    courseStudentDOS.get(i).setIsEvaluation(0);
                }else {
                    courseStudentDOS.get(i).setEvaluationTime(evaluationDO.getEvaluationTime());
                    courseStudentDOS.get(i).setIsEvaluation(1);
                }
            }
            int total = evaluationService.countStuFeedback(query);
            PageUtils pageUtils = new PageUtils(courseStudentDOS,total);
            return pageUtils;
        }else {
            //有查询条件,获取所查询课程的课程id
            int courseId = courseDO1.getCid();
            params.put("courseId",courseId);
            Query query = new Query(params);
            List<CourseStudentDO> courseStudentDOS = evaluationService.selectStuFeedback(params);
            for (int i=0;i<courseStudentDOS.size();i++){
                int cid = courseStudentDOS.get(i).getCid();
                CourseDO courseDO = courseService.selectById(cid);
                String time1 = courseDO.getStartDate();
                String time2 = courseDO.getEndDate();
                //课程名称
                courseStudentDOS.get(i).setCourseName(courseDO.getCourseName());
                courseStudentDOS.get(i).setStartDate(time1);
                courseStudentDOS.get(i).setEndDate(time2);
                //如果想比较日期则写成"yyyy-MM-dd"就可以了
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                //将字符串形式的时间转化为Date类型的时间
                //开始时间
                Date a=sdf.parse(time1);
                //结束时间
                Date b=sdf.parse(time2);
                //当前时间
                Date date = new Date();
                String d = sdf.format(date);
                Date c = sdf.parse(d);
                Integer value = DateCompareUtils.isEffectiveDate(c,a,b);
                courseStudentDOS.get(i).setStatus(value);
                //学生id
                int stu_id = courseStudentDOS.get(i).getUserId();
                String name = courseService.getNameById(stu_id);
                courseStudentDOS.get(i).setName(name);
                EvaluationDO evaluationDO = evaluationService.selectByUserId(stu_id,loginId);
                if (evaluationDO == null){
                    //未评价
                    courseStudentDOS.get(i).setIsEvaluation(0);
                }else {
                    courseStudentDOS.get(i).setEvaluationTime(evaluationDO.getEvaluationTime());
                    courseStudentDOS.get(i).setIsEvaluation(1);
                }
            }
            int total = evaluationService.countStuFeedback(query);
            PageUtils pageUtils = new PageUtils(courseStudentDOS,total);
            return pageUtils;
        }
    }

    @GetMapping("/listStu/{value}")
    public String listStu(Model model,@PathVariable("value")Integer value){
        //获取前台点击的学生id
        int id = value;
        //通过id查询学生课程信息
        CourseStudentDO courseStudentDO = courseStudentService.selectById(id);
        //学生id，也就是评价人id
        int stu_id = courseStudentDO.getUserId();
        //获取登录人信息
        UserDO userDO = ShiroUtils.getUser();
        //教师id，也就是被评价人id
        int tea_id = Integer.parseInt(Long.toString(userDO.getUserId()));
        //查询评价记录
        EvaluationDO evaluationDO = evaluationService.selectByUserId(stu_id,tea_id);
        //将评价信息返回给前台页面
        model.addAttribute("evaluationDO",evaluationDO);
        return "sys/feedback/evaluationStudent";
    }

    //教师意见反馈列表
    @GetMapping("/teaFeedback")
    public String teaFeedback(){
        return "sys/feedback/teaFeedback";
    }

    @GetMapping("/listTeaFeedback")
    @ResponseBody
    PageUtils listTeaFeedback(@RequestParam Map<String,Object> params) throws Exception{
        UserDO userDO = ShiroUtils.getUser();
        int loginId = Integer.parseInt(Long.toString(userDO.getUserId()));
        params.put("loginId",loginId);
        //传过来的查询内容
        String course_name = String.valueOf(params.get("condition"));
        CourseDO courseDO1 = courseService.selectCourseByCid(course_name);
        if (courseDO1 == null ){
            Query query = new Query(params);
            List<UserCourseDo> userCourseDos = evaluationService.selectTeaFeedback(params);
            for (int i=0;i<userCourseDos.size();i++){
                String courseNo = userCourseDos.get(i).getCourseNo();
                CourseDO courseDO = courseService.selectCourseByNo(courseNo);
                String time1 = courseDO.getStartDate();
                String time2 = courseDO.getEndDate();
                //课程名称
                userCourseDos.get(i).setCourseName(courseDO.getCourseName());
                userCourseDos.get(i).setStartDate(time1);
                userCourseDos.get(i).setEndDate(time2);
                //如果想比较日期则写成"yyyy-MM-dd"就可以了
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                //将字符串形式的时间转化为Date类型的时间
                //开始时间
                Date a=sdf.parse(time1);
                //结束时间
                Date b=sdf.parse(time2);
                //当前时间
                Date date = new Date();
                String d = sdf.format(date);
                Date c = sdf.parse(d);
                Integer value = DateCompareUtils.isEffectiveDate(c,a,b);
                userCourseDos.get(i).setStatus(value);
                //教师id
                int tea_id = userCourseDos.get(i).getUserId();
                String name = courseService.getNameById(tea_id);
                userCourseDos.get(i).setName(name);
                EvaluationDO evaluationDO = evaluationService.selectByUserId(tea_id,loginId);
                if (evaluationDO == null){
                    //未评价
                    userCourseDos.get(i).setIsEvaluation(0);
                }else {
                    userCourseDos.get(i).setEvaluationTime(evaluationDO.getEvaluationTime());
                    userCourseDos.get(i).setIsEvaluation(1);
                }
            }
            int total = evaluationService.countTeaFeedback(query);
            PageUtils pageUtils = new PageUtils(userCourseDos,total);
            return pageUtils;
        }else {
            String courseNo1 = courseDO1.getCourseNo();
            params.put("courseNo",courseNo1);
            Query query = new Query(params);
            List<UserCourseDo> userCourseDos = evaluationService.selectTeaFeedback(params);
            for (int i=0;i<userCourseDos.size();i++){
                String  courseNo = userCourseDos.get(i).getCourseNo();
                CourseDO courseDO = courseService.selectCourseByNo(courseNo);
                String time1 = courseDO.getStartDate();
                String time2 = courseDO.getEndDate();
                //课程名称
                userCourseDos.get(i).setCourseName(courseDO.getCourseName());
                userCourseDos.get(i).setStartDate(time1);
                userCourseDos.get(i).setEndDate(time2);
                //如果想比较日期则写成"yyyy-MM-dd"就可以了
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                //将字符串形式的时间转化为Date类型的时间
                //开始时间
                Date a=sdf.parse(time1);
                //结束时间
                Date b=sdf.parse(time2);
                //当前时间
                Date date = new Date();
                String d = sdf.format(date);
                Date c = sdf.parse(d);
                Integer value = DateCompareUtils.isEffectiveDate(c,a,b);
                userCourseDos.get(i).setStatus(value);
                //教师id
                int tea_id = userCourseDos.get(i).getUserId();
                String name = courseService.getNameById(tea_id);
                userCourseDos.get(i).setName(name);
                EvaluationDO evaluationDO = evaluationService.selectByUserId(tea_id,loginId);
                if (evaluationDO == null){
                    //未评价
                    userCourseDos.get(i).setIsEvaluation(0);
                }else {
                    userCourseDos.get(i).setEvaluationTime(evaluationDO.getEvaluationTime());
                    userCourseDos.get(i).setIsEvaluation(1);
                }
            }
            int total = evaluationService.countTeaFeedback(query);
            PageUtils pageUtils = new PageUtils(userCourseDos,total);
            return pageUtils;
        }
    }

    @GetMapping("/listTea/{value}")
    public String listTea(Model model,@PathVariable("value")Integer value){
        int id = value;
        UserCourseDo userCourseDo = evaluationService.selectUserCourseById(id);
        //教师id，也就是评价人id
        int tea_id = userCourseDo.getUserId();
        UserDO userDO = ShiroUtils.getUser();
        //学生id，也就是被评价人id
        int stu_id = Integer.parseInt(Long.toString(userDO.getUserId()));
        //查询评价记录
        EvaluationDO evaluationDO = evaluationService.selectByUserId(tea_id,stu_id);
        model.addAttribute("evaluationDO",evaluationDO);
        return "sys/feedback/evaluationTeacher";
    }


}
